package cn.tedu.charging.order.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class RabbitConfiguration {

    public static final String EXCHANGE_NAME = "charging_order_exchange";
    public static final String ROUTING_KEY = "charging_order_routing_key";
    private static final String QUEUE_NAME = "charging_order_queue";

    private static final String DEAD_LETTER_EXCHANGE_NAME = "dead_letter_charging_order_exchange";
    private static final String DEAD_LETTER_ROUTING_KEY = "dead_letter_charging_order_routing_key";
    public static final String DEAD_LETTER_QUEUE_NAME = "dead_letter_charging_order_queue";

    /**
     * 通过Spirng方式@Bean创建 DirectExchange
     * @return
     */
    @Bean
    public DirectExchange exchange(){
        //消息持久化
        Boolean durable = true;
        //队列是否自动删除
        Boolean autoDelete = false;
        return new DirectExchange(EXCHANGE_NAME,durable,autoDelete);
    }

    /**
     * 通过Spirng方式@Bean创建 Queue
     * @return
     */
    @Bean
    public Queue queue(){
        Map<String,Object> args = new HashMap<>();
        //模拟时间 设置消息的TTl 为两分钟 模拟充电充满需要2分钟
        Integer ttl = 2 * 60 * 1000;
        args.put("x-message-ttl",ttl);
        //设置正常队列的死信队列的Exchange
        args.put("x-dead-letter-exchange",DEAD_LETTER_EXCHANGE_NAME);
        //设置正常队列的死信队列的RoutingKey
        args.put("x-dead-letter-routing-key",DEAD_LETTER_ROUTING_KEY);
        //消息持久化
        Boolean durable = true;
        //互斥 连接级别的互斥
        Boolean exclusive = false;
        //队列是否自动删除
        Boolean autoDelete = false;
        return new Queue(QUEUE_NAME,durable,exclusive,autoDelete,args);
    }

    /**
     * 通过Spirng方式@Bean 把Exchange和Queue通过RoutingKey绑定
     * @return
     */
    @Bean
    public Binding orderBind(){
        return BindingBuilder.bind(queue()).
                to(exchange()).with(ROUTING_KEY);
    }

    /**
     * 通过Spirng方式@Bean创建 死信DirectExchange
     * @return
     */
    @Bean
    public DirectExchange deadLetterExchange(){
        //消息持久化
        Boolean durable = true;
        //队列是否自动删除
        Boolean autoDelete = false;
        return new DirectExchange(DEAD_LETTER_EXCHANGE_NAME,durable,autoDelete);
    }

    /**
     * 通过Spirng方式@Bean创建 死信Queue
     * @return
     */
    @Bean
    public Queue deadLetterQueue(){
        //消息持久化
        Boolean durable = true;
        //队列是否自动删除
        Boolean autoDelete = false;
        return new Queue(DEAD_LETTER_QUEUE_NAME,durable,false,autoDelete);
    }

    /**
     * 通过Spirng方式@Bean 把死信 Exchange和死信 Queue通过 死信 RoutingKey绑定
     * @return
     */
    @Bean
    public Binding deadLetterOrderBind(){
        return BindingBuilder.bind(deadLetterQueue()).
                to(deadLetterExchange()).with(DEAD_LETTER_ROUTING_KEY);
    }


}
